package fr.efrei.musicpliz.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import fr.efrei.musicpliz.dao.AbstractDao;
import fr.efrei.musicpliz.entity.AbstractEntity;

/**
 * Implementation of AbstractDao
 */
public abstract class AbstractDaoImpl<E extends AbstractEntity> implements AbstractDao<E> {
	
	// small helper for receiving the class-information from the instance of e
	private transient E e;
	
	@PersistenceContext
	protected EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public E get(int id) {
		return (E)entityManager.find(e.getClass(), id);
	}
	
	public void delete(E e) {
		entityManager.remove(entityManager.getReference(e.getClass(), e.getId()));
	}
	
	public E save(E e) {
		return entityManager.merge(e);
	}
	
}
